/**
 * 2019-05-19 17:18:04
 */
package com.cdxy.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Esion
 */
public class CommonUtil {
    private static final transient Logger log = LoggerFactory.getLogger(CommonUtil.class);

    /**
     * 校验手机号
     *
     * @param phone
     * @return
     */
    public static boolean checkPhone(String phone) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            log.info(phone + " 是" + (isMatch ? "有效" : "无效") + "手机号");
            if (isMatch) {
                return true;
            } else {
                return false;
            }
        }
    }

    public static boolean checkEmail(String email) {
        String regex = "^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\\.)+(com|cn|net|org)$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(email);
        boolean isMatch = m.matches();
        if (isMatch) {
            return true;
        } else {
            return false;
        }
    }

    /**
     *  获取id地址
     * @return
     */
    public static String getIpAddress() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getRemoteAddr();
    }

    public static boolean isIdcard(String cardNo) {
        if (cardNo.length() != 18) {
            return false;
        }

        Pattern pattern = Pattern.compile("^(\\d{6})(19|20)(\\d{2})(1[0-2]|0[1-9])(0[1-9]|[1-2][0-9]|3[0-1])(\\d{3})(\\d|X|x)?$");
        Matcher matcher = pattern.matcher(cardNo);

        if (!matcher.matches()) {
            return false;
        }

        String matchDigit = "";
        // 1.将身份证号码前面的17位数分别乘以不同的系数。从第一位到第十七位的系数分别为：7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
        int[] intArr = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
        int sum = 0;
        for (int i = 0; i < intArr.length; i++) {
            // 2.将这17位数字和系数相乘的结果相加。
            sum += Character.digit(cardNo.charAt(i), 10) * intArr[i];
        }
        // 3.用加出来和除以11，看余数是多少？
        int mod = sum % 11;
        // 4.余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2。
        int[] intArr2 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        int[] intArr3 = { 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 };

        for (int i = 0; i < intArr2.length; i++) {
            int j = intArr2[i];
            if (j == mod) {
                if (intArr3[i] > 57) {
                    matchDigit = String.valueOf((char) intArr3[i]);
                } else {
                    matchDigit = String.valueOf(intArr3[i]);
                }
            }
        }

        if (matchDigit.equals(cardNo.substring(cardNo.length() - 1))) {
            return true;
        } else {
            return false;
        }

    }
}
